package MyQueue;

/**
 * Description:
 * Date: 2025-11-10
 * Time: 20:13
 */
//双向链表实现队列
public class DoubleLinkedList {
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;
    private ListNode last;
    private int usedSzie;

    public void offer(int data) {
        ListNode node = new ListNode(data);
        if (isEmpty()) {
            this.head = this.last = node;
            this.usedSzie++;
            return;
        }
        last.next = node;
        node.prev = last;
        last = last.next;
        this.usedSzie++;
    }

    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        int val = this.head.val;
        this.head = this.head.next;
        if (this.head !=null) {
            this.head.prev = null;
        }
        this.usedSzie--;
        return val;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return this.head.val;
    }

    public int size() {
        return this.usedSzie;
    }

    public boolean isEmpty() {
        return this.usedSzie == 0;
    }

    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
